package com.google.code.greenplate;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MyLocationSetup extends Activity {

	//private static final String TAG = "MyLocationSetup";

	// Global variables
	int location_counter = 0;
	TextView tv = null;
	private GPSListener myGPSListener = null;
	private ProgressDialog mProgressDialog = null;
	
	// Repetitively checks if the location has been found.
	private Handler locHandler;
		
	// 1 second between checks for location
	private static final int LOCATION_CHECK_INTERVAL_MILLIS = 1000;
	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.map_counter);
        
        tv = (TextView)findViewById (R.id.counter);

        myGPSListener = new GPSListener(this);
                
        // Check if this device can find its location. If not, display error
        if (myGPSListener.getNetworkStatus()){
        	 myGPSListener.getLocation();
        } else if (myGPSListener.getGPSStatus() ){
        	myGPSListener.getLocation();
        } else {
        	tv.setText("This device cannot find its location.");
        	return;
        }        

		// Repeatedly checks if the location has been found. If found, display lat,lng
        // on map in MyLocationMap activity and end this activity. If not found 
        // before count to 20 ends, then this thread is killed and a error message 
        // is displayed on the screen 
		locHandler = new Handler();

		//Thread tLoc = new Thread(locUpdate);
		Thread tLoc = new Thread() {
			public void run() {
				locHandler.postDelayed(locUpdate, LOCATION_CHECK_INTERVAL_MILLIS);
			}
		};
		tLoc.start();

		mProgressDialog = ProgressDialog.show(MyLocationSetup.this,    
    			"Retrieving location...", "Click Back button to stop.", true, true, new pdCancel());		
	}
		

	private class pdCancel implements OnCancelListener {
		public void onCancel(DialogInterface dialog) {
			finish();
		}
	}

	// Last call before activity quits
	@Override
	public void onDestroy() {
		super.onDestroy();
		// cancel location polling thread
        if (locHandler != null)
        	locHandler.removeCallbacks(locUpdate);
		// Stop reporting change in GPS location
        if (myGPSListener != null) {
        	myGPSListener.pauseLocation();
        }
  	}

	
	// Poll for GPS/Network Location
	final Runnable locUpdate = new Runnable() {
		public void run() {
			locHandler.removeCallbacks(locUpdate);
			location_counter++;
			tv.setText(location_counter+"");
			if (myGPSListener.foundLocation()) {
                Intent intent = new Intent();
                intent.setClass(MyLocationSetup.this, MyLocationMap.class);
                intent.putExtra("Lat", myGPSListener.getLat());
                intent.putExtra("Lng", myGPSListener.getLng());
                mProgressDialog.dismiss();
                startActivity(intent);
				finish();
			} else {
				if (location_counter < 20)
					// schedule an update to recheck the location
					locHandler.postDelayed(locUpdate, LOCATION_CHECK_INTERVAL_MILLIS);
				else {
					tv.setText("Could not find location.");
					// Stop reporting change in GPS location
					mProgressDialog.dismiss();
			        if (myGPSListener != null) {
			        	myGPSListener.pauseLocation();
			        }
				}
			}
		}
	};
}
